﻿// =================================================================== 
// 项目说明
//====================================================================
// lsc @Copy Right 2020
// 文件： LiveRoomRepository.cs
// 项目名称：斑马车务
// 创建时间：2020/2/24
// 负责人：lsc
// ===================================================================
using System;
using System.Text;
using System.Collections.Generic;
using YouWei.Infrastructure.Entity;
using Dapper.Extensions;
using YouWei.Infrastructure.Models;
using Dapper;

namespace YouWei.Infrastructure.Repository
{
	/// <summary>
	/// 仓储层
	/// </summary>	
	public class LiveRoomRepository:BaseRepository<LiveRoom>
	{		
    
        #region 构造

        public LiveRoomRepository() : base(){}

        public LiveRoomRepository(string connectionString) : base(connectionString){}

        public LiveRoomRepository(DatabaseContext dbContext) : base(dbContext){}

        #endregion

        /// <summary>
        /// 分页查询，带条件
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="condition"></param>
        /// <returns></returns>
        public Page<LiveRoom> GetPages(int pageIndex, long pageSize, ReqLiveRoomCondition condition)
        {
            StringBuilder sql = new StringBuilder();
            if (condition.PlayerUserID > 0)
            {
                sql.Append("SELECT room.* FROM LiveRoom room WITH(NOLOCK) LEFT JOIN LiveConcern con WITH(NOLOCK) ON room.UserID = con.PusherUserID where room.IsActive = 1 AND con.IsActive = 1 AND con.PlayerUserID = @PlayerUserID");
            }
            else
            {
                sql.Append("SELECT * FROM LiveRoom WITH(NOLOCK) where IsActive =1");
            }
            DynamicParameters dparams = new DynamicParameters();
            if (!string.IsNullOrWhiteSpace(condition.StartDate))
            {
                dparams.Add("@StartDate", condition.StartDate, System.Data.DbType.String);
                sql.Append(" AND CreatedTime>@StartDate");
            }
            if (!string.IsNullOrWhiteSpace(condition.EndDate))
            {
                dparams.Add("@EndDate", condition.EndDate, System.Data.DbType.String);
                sql.Append(" AND CreatedTime<@EndDate");
            }
            if (!string.IsNullOrWhiteSpace(condition.RoomName))
            {
                dparams.Add("@RoomName", condition.RoomName, System.Data.DbType.String);
                sql.Append(" AND Name like '%@RoomName%'");
            }

            if (condition.PlayerUserID > 0)
            {
                sql.Append(" AND Status=@PlayerUserID");
                dparams.Add("@PlayerUserID", condition.PlayerUserID, System.Data.DbType.Int32);
            }

            sql.Append(" AND Status<3 ORDER BY Status DESC");
            
            return dbContext.Connection.Page<LiveRoom>(pageIndex, pageSize, sql.ToString(), dparams);
        }

        /// <summary>
        /// 获取用户最后一次直播室
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public LiveRoom GetLastLiveRoomByUserId(int userId)
        {
            string sql = "select top 1 * from LiveRoom where IsActive=1 and UserID=@UserID order by RoomID DESC";
            LiveRoom obj = SingleOrDefault(sql, new { UserID = userId });
            return obj;
        }

        /// <summary>
        /// 更新直播室状态
        /// </summary>
        /// <param name="roomId"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public bool UpdateStatusForLiveRoom(int roomId,int status)
        {
            string sql = "update LiveRoom set Status=@Status,UpdatedTime=getdate() where IsActive=1 and RoomID=@RoomID";
            int result = Execute(sql, new { RoomID = roomId, Status = status });
            return result>0;
        }

    }
}